__version__ = "1.1.0"
# https://blog.csdn.net/weixin_43790276/article/details/101944628
# when： 指定日志文件轮转的时间单位
#     S - Seconds
#     M - Minutes
#     H - Hours
#     D - Days
#     midnight - roll over at midnight
#     W{0-6} - roll over on a certain day; 0 - Monday
import datetime
import logging
import time
from logging.handlers import TimedRotatingFileHandler

formatter = '%(asctime)s -<>- %(filename)s -<>- [line]:%(lineno)d -<>- %(levelname)s -<>- %(message)s'


def logfile_hander(path='daily.log'):
    # time_rotate_file = TimedRotatingFileHandler(filename='time_rotate', when='S', interval=3, backupCount=5) # 测试每3秒，一个日志文件。
    # time_rotate_file = TimedRotatingFileHandler(filename='daily.log', when='D', backupCount=5)
    half_night = datetime.time(hour=6)
    time_rotate_file = TimedRotatingFileHandler(filename=path, when='midnight', atTime=half_night, backupCount=5)
    time_rotate_file.setFormatter(logging.Formatter(formatter))
    time_rotate_file.setLevel(logging.INFO)
    return time_rotate_file


def console_handler():
    console_handler = logging.StreamHandler()
    console_handler.setLevel(level=logging.INFO)
    console_handler.setFormatter(logging.Formatter(formatter))
    return console_handler


if __name__ == "__main__":
    logger = logging.getLogger(__name__)
    logger.setLevel(level=logging.INFO)
    logger.addHandler(logfile_hander())
    logger.addHandler(console_handler())

    while True:
        logger.info('info')
        logger.error('error')
        time.sleep(1)
